package provider

import (
	"time"
	"github.com/kingwrcy/devNews/model"
	"net/http"
	"crypto/sha1"
	"fmt"
	"log"
	"github.com/PuerkitoBio/goquery"
	"strings"
	url2 "net/url"
	"crypto/tls"
	"regexp"
)


type MaNongProvider struct{}

func (p *MaNongProvider) Schedule() time.Duration {
	return time.Hour*24*5
}

func (p *MaNongProvider) Name() string{
	return "moNong"
}


func (p *MaNongProvider) GetData(t time.Time) []model.News {

	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

	client := &http.Client{Transport: tr}

	news := []model.News{}
	url := "https://weekly.manong.io/issues/"
	resp, err := client.Get(url)
	if err != nil {
		log.Printf("get %s error : %s", url, err)
		return nil
	}
	defer resp.Body.Close()

	doc, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		log.Printf("NewDocumentFromReader error :%s\n",err)
		return nil
	}
	u, exists := doc.Find("h4").First().Find("a").Attr("href")
	if exists {
		resp, err = client.Get(u)
		if err != nil {
			log.Printf("get %s error : %s", u, err)
			return nil
		}
		defer resp.Body.Close()
		doc, err = goquery.NewDocumentFromReader(resp.Body)
		if err != nil {
			log.Printf("NewDocumentFromReader error : %s", err)
			return nil
		}

		 doc.Find("h4").Each(func(i int, s *goquery.Selection) {
			 first := s.Find("a").First()
			 href, exists := first.Attr("href")
			 href = strings.Replace(href,"https://weekly.manong.io/bounce?url=","",1)
			 reg := regexp.MustCompile("&aid=\\d+&nid=\\d+")
			 href = reg.ReplaceAllString(href,"")
			 href,_ = url2.QueryUnescape(href)
			 if exists {
				 h := sha1.New()
				 h.Write([]byte(fmt.Sprintf("%s%s", first.Text(), href)))
				 uuid := fmt.Sprintf("%x", h.Sum(nil))
				 n := model.News{
					 Title:   first.Text(),
					 Created: time.Now(),
					 Url:     href,
					 Source:  MaNong,
					 Uuid:    uuid,
				 }
				 news = append(news, n)
			 }
		 })
	}

	return news
}
